package com.factory.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.factory.common.bean.Manager;
import com.factory.common.core.BaseService;
import com.factory.common.pojo.Result;
import com.factory.common.utils.DateUtil;
import com.factory.common.utils.FileUtils;
import com.factory.common.utils.StringUtil;
import com.factory.manager.pojo.ManagerPojo;
import com.factory.manager.service.ManagerService;
import com.factory.manager.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/manager/manager")
@Slf4j
public class ManagerController extends BaseController<ManagerPojo>{

    @Autowired
    private ManagerService managerService;

    @Value("${jiexun.upload.dir}")
    private String dir;

    /**
     * 用户登录接口
     * @param param
     * @return
     */
    @PostMapping(value = "/login")
    public Result login(@RequestBody JSONObject param) {
        log.info("用户登录接口,{}",param);
        if(StringUtil.isEmpty(param.getString("username"))){
            return Result.fail("用户名不能为空");
        }
        if(StringUtil.isEmpty(param.getString("password"))){
            return Result.fail("用户密码不能为空");
        }
        return managerService.login(param.getString("username"), param.getString("password"));
    }

    /**
     * 用户退出登录接口
     * @return
     */
    @GetMapping(value = "/logout")
    public Result logout() {
        log.info("用户退出登录接口");
        return managerService.logout();
    }

    @Override
    protected BaseService getBaseService() {
        return managerService;
    }

    @Override
    public Result insert(@RequestBody ManagerPojo managerPojo) {

        if(StringUtil.notEmpty(managerPojo.getTempProfile())){
            String filePath =  DateUtil.getDateString(new Date(), "yyyy-MM-dd");
            String fileName = FileUtils.move(dir + File.separator + managerPojo.getTempProfile(), dir + File.separator + filePath);
            if(null != fileName){
                managerPojo.setProfile(filePath + File.separator + fileName);
            }
        }

        if(StringUtil.notEmpty(managerPojo.getPassword())){
            managerPojo.setPassword(DigestUtils.md5DigestAsHex(managerPojo.getPassword().getBytes()));
        }

        managerPojo.setId(StringUtil.getUUID());
        managerPojo.setCreateDate(new Date());
        managerService.insert(managerPojo);

        return Result.success(null,"操作成功!");
    }

    @Override
    public Result update(@RequestBody ManagerPojo managerPojo) {
        if(StringUtil.isEmpty(managerPojo.getId())){
            return Result.fail("id不能为空!");
        }
        if(StringUtil.notEmpty(managerPojo.getTempProfile())){
            String filePath =  DateUtil.getDateString(new Date(), "yyyy-MM-dd");
            String fileName = FileUtils.move(dir + File.separator + managerPojo.getTempProfile(), dir + File.separator + filePath);
            if(null != fileName){
                managerPojo.setProfile(filePath + File.separator + fileName);
            }
        }

        if(StringUtil.notEmpty(managerPojo.getPassword())){
            managerPojo.setPassword(DigestUtils.md5DigestAsHex(managerPojo.getPassword().getBytes()));
        }else
            managerPojo.setPassword(null);

        managerService.updateById(managerPojo);

        return Result.success(null,"操作成功!");
    }

    /**
     * 管理员重置密码接口
     * @param param
     * @return
     */
    @PostMapping(value = "/reset")
    public Result reset(@RequestBody JSONObject param) {
        log.info("管理员重置密码接口,{}",param);
        Manager manager = managerService.getLoginUser().get();

        if(StringUtil.isEmpty(param.getString("password"))){
            return Result.fail("密码不能为空!");
        }

        String password = DigestUtils.md5DigestAsHex(param.getString("oldPassword").getBytes());
        if(StringUtil.isEmpty(password)){
            return Result.fail("旧密码不能为空!");
        }else if(!password.equalsIgnoreCase(manager.getPassword())){
            return Result.fail("密码不匹配!");
        }
        manager.setPassword(password);
        managerService.updateById(manager);

        return Result.success(null,"操作成功!");
    }
}
